package singleton;

import java.util.concurrent.TimeUnit;

public class SingletonTest {
    public static void main(String[] args) throws InterruptedException {
        Singleton instance = Singleton.getInstance();
        System.out.println(instance.toString());
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Singleton instance1 = Singleton.getInstance();
            instance1.setName("99999");
            System.out.println(instance1.toString());
        }).start();

        TimeUnit.SECONDS.sleep(8);
        System.out.println(instance.toString());
    }
}
class Singleton{
    private String name;

    private static Singleton instance = new Singleton("6666");

    private Singleton(){}
    private Singleton(String name){this.name = name;}

    public static Singleton getInstance() {
        return instance;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Singleton{" +
                "name='" + name + '\'' +
                '}';
    }
}
